package cn.itsource.pethome.basic.service.impl;

import cn.itsource.pethome.basic.mapper.BaseMapper;
import cn.itsource.pethome.basic.service.IBaseService;
import cn.itsource.pethome.basic.util.BaseQuery;
import cn.itsource.pethome.basic.util.PageBase;
import cn.itsource.pethome.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
//类级别事务为读事务,分别在增删改的方法上面加写事务
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;
    @Override
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public void deleteAll(List<Long> id) {
        baseMapper.deleteAll(id);
    }

    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T findById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public PageBase<T> findAll(BaseQuery query) {
        Long count = baseMapper.findCount(query);
        if (count.equals(0L)){
            return new PageBase<T>();
        }
        List<T> all = baseMapper.findAll(query);
        return new PageBase<T>(all,count);

    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }
}
